<?php
//	Require the base HTTPApi
require 'HTTPApi.php';


class solomonAPI extends httpapi{

	private static function handle_status($response){
		//	If we get a 500 error then we need to build an error for it
		//echo($response['status']['code']);
		
		$prefix=substr($response['status']['code'], 0, -2);
		$suffix=substr($response['status']['code'], 1, 2);
		
		//	Switch on prefix
		switch($prefix){
			case '5':$response=solomonAPI::handle_5xx($response,$prefix,$suffix);break;
		}
		
		//	If its any 200 then return response
		return $response;
	}
	
	// private static function handle_1xx($response,$suffix){
	// 	return $response;
	// }
	// private static function handle_2xx($response,$suffix){
	// 	return $response;
	// }
	// private static function handle_3xx($response,$suffix){
	// 	return $response;
	// }
	// 
	// private static function handle_4xx($response,$suffix){
	// 	return $response;
	// }
	
	private static function handle_5xx($response,$prefix,$suffix){
		//	Switch the suffix
		$error=array(
			"response" => $response,
			"prefix" => $prefix,
			"suffix" => $suffix,
			"definition" => "Internal Server Error",
			"message" => "A generic error message, given when no more specific message is suitable."
		);
		return $error;
	}
	
	private static function makeRequest($config,$url,$params=array(),$_headers=array()){
		$headers = array(
			"Accept"=>"application/".$config['type'],
			"Content-Type"=>"application/".$config['type'],
			"Authorization" => "Basic ".base64_encode($config['username'].':'.$config['password'])
		);
		
		//	Add parameter headers to auth
		foreach($_headers as $key => $value){
			$headers[$key]=$value;
		}
		
		//	Get response
		$response=solomonAPI::makeGetRequest(
			array(
				"host" => $config['host'],
				"port" => $config['port'],
				"username" => $config['username'],
				"password" => $config['password'],
				"url" => $url
			),$params,$headers
		);
		
		$response=solomonAPI::handle_status($response);
		return $response;
	}
	
	public static function getUsers($config,$filter=array()){
		$params=$filter;
		$response=solomonAPI::makeRequest($config,'/api/users',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getUser($config,$user_id){
		$response=solomonAPI::makeRequest($config,'/api/users/'.$user_id,$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getContacts($config,$filter=array()){
		$params=$filter;
		$response=solomonAPI::makeRequest($config,'/api/contacts',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getContact($config,$contact_id){
		$response=solomonAPI::makeRequest($config,'/api/contacts/'.$contact_id);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getTagsForContact($config,$contact_id){
		$response=solomonAPI::makeRequest($config,'/api/contacts/'.$contact_id.'/tags');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getEmailTypes($config){
		$response=solomonAPI::makeRequest($config,'/api/email-types/');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getTelephoneTypes($config){
		$response=solomonAPI::makeRequest($config,'/api/telephone-types/');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getWebsiteTypes($config){
		$response=solomonAPI::makeRequest($config,'/api/website-types/');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	public static function getAddressTypes($config){
		$response=solomonAPI::makeRequest($config,'/api/address-types/');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	//	Activities
	// /api/activities?pagesize="
	public static function getActvity($config,$filter=array()){
		$params=$filter;
		$response=solomonAPI::makeRequest($config,'/api/activities',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	//	Notes methods
	public static function getNotesForContact($config,$contact_id,$paging=array()){
		$params=$paging;
		//get_notes_for_contact
		$response=solomonAPI::makeRequest($config,'/api/contacts/'.$contact_id.'/notes',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	
	//	Tasks methods
	public static function getTasksForContact($config,$contact_id,$paging=array()){
		$params=$paging;
		//get_notes_for_contact
		$response=solomonAPI::makeRequest($config,'/api/contacts/'.$contact_id.'/tasks',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	//	Tasks methods
	public static function getTasksForUser($config,$user_id,$filter=array()){
		$params=$filter;
		//get_notes_for_contact
		$response=solomonAPI::makeRequest($config,'/api/users/'.$user_id.'/tasks',$params);
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	
	public static function getTaskTypes($config){
		$response=solomonAPI::makeRequest($config,'/api/task-types/');
		if($response['response']!=null){
			if($response['suffix']=='00'){}
		}else{
			return json_decode($response['body']);
		}
	}
	
	
	
}
?>